/* Author: 
	Nicolaj Roos
*/
var current = 0;
var numberOfImages = 0;
var bodyWidth;
var bodyHeight;
var imgWidthOrg;
var imgHeightOrg;
var imgWidth;
var imgHeight;
var ratio;
var counter = 0;
var currentImage, oldImage;
var images;
var footerTxt;

var tempImage, tempBody;

var $xml;
var y = 0;


function formFadeIn(){
	$('#lightbox').fadeTo(500, 0.5);
	$('#form').fadeTo(500, 0.85);
	$(document).unbind('keydown', handler);
}

function formFadeOut () {
	
	
	$('#form, #lightbox').fadeOut(500, function(){
		$(document).unbind('keydown', handler).bind('keydown', handler);
	});
}

$(window).load(function(){
		
	$.get('cms/getSQL.php', function(d){
		$xml = $(d);
		continueLoad();
		//console.log($(d).find('image[sort=1]').text());	
		}
	);
		
});

function continueLoad(){
	/*
	var footerHeight = $('footer').height();
	var	footerElementHeight = $('#footerElement').height();
	var footerMargin = (footerHeight - footerElementHeight)/2;
	console.log("Her: "+footerMargin);
	$('#footerElement').css('top', footerMargin);
	*/
	$('#contactForm').submit( submitForm );
	$('#contactForm #cancel').click( function(e){
		e.preventDefault();
		formFadeOut();
		
	} );

	currentImage = new Image();
	oldImage = new Image();	

	numberOfImages = $xml.find('image').length-1;
	
	currentImage.src = getImageUrl(current);
	
	currentImage.onload = function() {
		
		imgWidthOrg = currentImage.width;
		imgHeightOrg = currentImage.height;
		
		$('#footerElement a').click(function(e){
			e.preventDefault();
			formFadeIn();
		});
			
		if(imgWidthOrg > $('#main').width() || imgHeightOrg > $('#main').height()){
	
			$('#main').css('background', 'url('+getImageUrl(current)+') center')
						.css('background-size', 'contain')
						.css('background-repeat', 'no-repeat');	
		} else {
			$('#main').css('background', 'url('+getImageUrl(current)+') center')
						.css('background-repeat', 'no-repeat')
						.css('background-size', 'auto');	
		};			
	}
}

function getImageUrl(sort){
	return "img/gallery/" + $xml.find("image[listorder="+sort+"] > filename").text();
}


//$(document).bind('keydown', handler);
$(document).unbind('keydown', handler).bind('keydown', handler);


function handler(e){
	if (e.keyCode == 37) { 
       prev();
       return false;
    } else if (e.keyCode == 39) { 
       next();
       return false;
    } else if (e.keyCode == 27) { 
		//document.context.webkitCancelFullScreen();
		document.context.webkitCancelFullScreen();
	}  else {
	
		//return false;
	}
}

function prev(){
	oldImage = currentImage;

	if(current == 0){
		current = numberOfImages;
	} else {
		current--;
	}

	showCurrent(oldImage);
}

function next(){
	//console.log("next(), nr."+ y++);
	oldImage = currentImage;

	if(current == numberOfImages){
		current = 0;
	} else {
		current++;
	}
	//footer("current: "+current);
	showCurrent(oldImage);
}

function showCurrent(oldImage){
				currentImage = new Image();
				
				currentImage.src = getImageUrl(current);
				
				currentImage.onload = function() {

					
  					imgWidthOrg = currentImage.width;
					imgHeightOrg = currentImage.height;
					
					// Fade gammelt indhold ud
					$('#main').fadeOut("slow", 
						function() { 		
							// Når fadeout er slut:
							// Fade nyt indhold ind	
							
							// *********************
							if(imgWidthOrg > $('#main').width() || imgHeightOrg > $('#main').height()){
								
									$('#main').css('background', 'url('+getImageUrl(current)+') center')
												.css('background-size', 'contain')
												.css('background-repeat', 'no-repeat');
							} else {
									$('#main').css('background', 'url('+getImageUrl(current)+') center')
												.css('background-size', 'auto')		
												.css('background-repeat', 'no-repeat');						
							};
							$('#main').fadeIn("slow");
						}
					);
				}
}

function footer(s){
	$('#footerElement').text(s);
}




var messageDelay = 2000;  // How long to display status messages (in milliseconds)

function submitForm() {
  var contactForm = $(this);

  // Are all the fields filled in?

  if ( !$('#senderName').val() || !$('#senderEmail').val() || !$('#message').val() ) {
    // No; display a warning message and return to the form
    $('#incompleteMessage').fadeIn().delay(messageDelay).fadeOut();

    contactForm.fadeOut().delay(messageDelay).fadeIn();

  } else {

    // Yes; submit the form to the PHP script via Ajax

    $('#sendingMessage').fadeIn();
    contactForm.fadeOut();

    $.ajax( {
      url: contactForm.attr( 'action' ) + "?ajax=true",
      type: contactForm.attr( 'method' ),
      data: contactForm.serialize(),
      success: submitFinished
    } );
  }

  // Prevent the default form submission occurring
  return false;
}


// Handle the Ajax response

function submitFinished( response ) {
  response = $.trim( response );
  $('#sendingMessage').fadeOut();

  if ( response == "success" ) {

    // Form submitted successfully:
    // 1. Display the success message
    // 2. Clear the form fields
    // 3. Fade the content back in

    $('#successMessage').fadeIn().delay(messageDelay).fadeOut();
    $('#senderName').val( "" );
    $('#senderEmail').val( "" );
    $('#message').val( "" );

    $('#content').delay(messageDelay+500).fadeTo( 'slow', 1 );

  } else {

    // Form submission failed: Display the failure message,
    // then redisplay the form
    $('#failureMessage').fadeIn().delay(messageDelay).fadeOut();
    $('#contactForm').delay(messageDelay+500).fadeIn();
  }
}